# -------------------------------------------------------
# Build stage
# -------------------------------------------------------
FROM gradle:8.6-jdk17 AS builder
WORKDIR /workspace

# 复制全部项目
COPY . .

# 构建参数：子模块路径，如 webapps/system-application
ARG MODULE
ENV MODULE=${MODULE}

# 1) 编译模块，不跑测试
RUN ./gradlew --no-daemon :${MODULE}:bootJar -x test || \
    ./gradlew --no-daemon :${MODULE}:jar -x test

# 2) 自动找到构建出来的 jar（bootJar 或普通 jar）
#   find 会自动找 build/libs 下的唯一 jar
RUN export JAR_FILE=$(find ${MODULE}/build/libs -maxdepth 1 -name "*.jar" | head -n 1) \
    && echo "BUILT_JAR=$JAR_FILE" > /workspace/jar.info


# -------------------------------------------------------
# Runtime stage（自动读取 jar.info）
# -------------------------------------------------------
FROM eclipse-temurin:17-jre AS runtime
WORKDIR /app

# 复制 jar 路径配置
COPY --from=builder /workspace/jar.info /app/jar.info
COPY --from=builder /workspace /workspace

# 自动读取 jar 路径
RUN JAR_PATH=$(cat /app/jar.info | cut -d '=' -f2) \
    && cp "/workspace/$JAR_PATH" /app/app.jar

ENTRYPOINT ["java", "-jar", "/app/app.jar"]
